package gae.google.tcserver.servlets;

import gae.google.tcserver.db.SaveToDB;
import gae.google.tcserver.db.Team;

import java.io.IOException;
//import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Key;


/**
 * The <tt>CreateTeamServlet</tt> class creates a new team
 * up on received team name, calendar name and image name. 
 *
 * @author Lars Friberg
 *
 * @date 2011-01-18
 */
public class CreateTeamServlet extends HttpServlet {

	
	/**
	 * serial version
	 */
	private static final long serialVersionUID = 5779324833580285672L;

	//private static final Logger log = Logger.getLogger(LoginServlet.class.getName());


	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// save all parameters received from CreateTeam.jsp
		String teamName = request.getParameter("teamName");
		String teamCalendar = request.getParameter("teamCalendar");
		String teamImage = request.getParameter("teamImage");
		
		if (teamName.isEmpty()){
			// dispatch to CreateTeam.jsp
			RequestDispatcher view = getServletContext().getRequestDispatcher("/views/CreateTeam.jsp");
			view.forward(request, response);
		}
		// get current user key Id from session attribute
		HttpSession session = request.getSession(false);
		Key userKeyId = (Key) session.getAttribute("userkeyid");

		// create team
		Team team = new Team(teamName, teamCalendar, teamImage, userKeyId);
		
		// save team in DB
		SaveToDB.saveTeam(team);

		// dispatch to TeamCalendar.jsp
		RequestDispatcher view = getServletContext().getRequestDispatcher("/views/TeamCalendar.jsp");
		view.forward(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException
	{
		// dispatch to Poll.jsp
		RequestDispatcher view = getServletContext().getRequestDispatcher("/views/TeamCalendar.jsp");
		view.forward(request, response);
	}
}
/*
	<%String teamId;%>
		<%String url;%>
		<%String image;%>
		<%for (TeamBean team : teams) {%>
			<%teamId = team.getId();%>
			<%url = "/views/TeamMembers.jsp?id=" + teamId;%>
			<%image = team.getImageName();%>
			<%if (image == null || image.contains("Image Name") || image.length() == 0){%>
				<%image = "../PICS/NoImageSelected.jpg";%>
			<%}else{%>
				<%image = "../PICS/" + team.getImageName();%>
		<%}%>
	 		<div id="teams">
			<p><%=team.getName()%></p>
	        <a href=<%=url%>>
			<img src=<%=image%> style="border:1px solid LightGrey; height: 150px; width: 200px"></img></a> 
		</div>
	<%}%>
*/